package com.logic.modular.game.player.info;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.exception.ServiceException;
import com.comment.pojo.domain.logic.domain.Player;
import com.logic.modular.game.player.info.mapper.PlayerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public abstract class PlayerBaseServiceImpl extends ServiceImpl<PlayerMapper, Player> implements IService<Player> {
    private static final String cacheKey = "player:%d:player";
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private ValueOperations<Object, Player> valueOperations;
    @Resource
    private HashOperations<Object, Object, Player> hashOperations;
    @Resource
    public PlayerMapper playerMapper;


    public Player select(Long playerId) {
        Player exitedAccount = this.selectCache(playerId);
        if (exitedAccount == null) {
            LambdaQueryWrapper<Player> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Player::getUid, playerId);
            exitedAccount = playerMapper.selectOne(wrapper);
            if (exitedAccount != null) {
                this.savaCache(exitedAccount);
            }
        }

        return exitedAccount;
    }

    public boolean updateById(Player entity) {
        boolean b = super.updateById(entity);
        if (b) {
            Player byId = playerMapper.selectById(entity.getUid());
            this.savaCache(byId);
        }
        return b;
    }

    public boolean save(Player entity) {
        if (entity.getUid() == null) {
            throw new ServiceException("uid不能为空");
        } else {
            entity.setCreateTime(new Date());
            playerMapper.insert(entity);
            entity = select(entity.getUid());
            this.savaCache(entity);
        }
        return false;
    }

    public void savaCache(Long keyValue1,@org.jetbrains.annotations.NotNull Map<String, Player> map) {
        String key1 = String.format(cacheKey, keyValue1);
        this.hashOperations.putAll(key1, map);
        this.redisTemplate.expire(key1, 60L, TimeUnit.MINUTES);
    }

    public void savaCache(Player entity) {
        String key1 = String.format(cacheKey, entity.getUid());
        this.valueOperations.set(key1, entity, 60L, TimeUnit.MINUTES);
    }

    public Player selectCache(@NotNull(message = "keyValue1不能为空") Long keyValue1) {
        String key1 = String.format(cacheKey, keyValue1);
        this.redisTemplate.expire(key1, 60L, TimeUnit.MINUTES);
        return this.valueOperations.get(key1);
    }

    public List<Player> listCache(@NotNull(
            message = "keyValue1不能为空"
    ) Long keyValue1) {
        String key1 = String.format(cacheKey, keyValue1);
        this.redisTemplate.expire(key1, 60L, TimeUnit.MINUTES);
        return new ArrayList<>(this.hashOperations.entries(key1).values());
    }

    public void removeCache(Player entity) {
        String key1 = String.format(cacheKey, entity.getUid());
        this.redisTemplate.delete(key1);
    }

}